//加载数据

$(document).ready(function () {
    loadData();
});


//总页数
let allPage = 1;

//加载数据
function loadData() {
    //获取请求参数
    let loadParams = getLoadParams();
    WeKnow.loading();
    //请求接口
    $.ajax({
        type: "POST",
        url: "prescriptionAudit/templates",
        dataType: "json",
        data: loadParams,
        async: true,//异步调用
        success: function (data) {
            WeKnow.closeLoading();
            if (data.errCode === 1) {
                let $orgBody = $('#tbodyTemplateList');
                $orgBody.empty();
                let dataInfo = data.templateList;
                if (dataInfo && dataInfo.length) {
                    let e = '';
                    $(dataInfo).each(function (idx, rowData) {
                        e += '<tr data-template_name="' + rowData.templateName + '" data-template_pic_url="' + rowData.templatePicUrl + '"  data-institution_id="' + rowData.institutionId + '">';
                        //序号
                        e += '<td class="text-center">' + (idx + 1) + '</td>';
                        // 机构名称
                        e += '<td class="text-center">' + (rowData.institutionName ? rowData.institutionName : "-") + '</td>';
                        // 模板名称
                        e += '<td class="text-center">' + (rowData.templateName ? rowData.templateName : "-") + '</td>';
                        // templateSign
                        e += '<td class="text-center">' + (rowData.templateSign ? rowData.templateSign : "-") + '</td>';
                        // 上传人员
                        e += '<td class="text-center">' + (rowData.uploaderName ? rowData.uploaderName : "-") + '</td>';
                        // 上传时间
                        e += '<td class="text-center">' + (rowData.uploadTmStr ? rowData.uploadTmStr : "-") + '</td>';

                        if (rowData.templatePicUrl != null) {
                            // 上传模板
                            e += '<td class="text-center">';
                            e += '<a href="#"><span class="showPrescriptionTemplateSpan templateSpan">查看</span></a>';
                            e += '</td>';
                            // 操作
                            e += '<td class="text-center">';
                            e += '<a href="#"><span class="downLoadTemplateSpan templateSpan">下载模板</span></a>';
                            e += '<a href="#"><span class="setTemplateSpan templateSpan">设置</span></a>';
                            e += '</td>';
                        } else {
                            // 处方模板
                            e += '<td class="text-center">';
                            e += '<a href="#"><span class="">/</span></a>';
                            e += '</td>';
                            // 操作
                            e += '<td class="text-center">';
                            e += '<a href="#"><span class="matchPrescriptionTemplateSpan">/</span></a>';
                            e += '</td>';
                        }


                        e += '</tr>';
                    });
                    $orgBody.append(e);
                } else {
                    WeKnow.notifyMsg('暂无数据');
                }
                //页码修改
                if (allPage != data.pages) {
                    allPage = data.pages;
                    //重新生成页面下拉
                    let $selectPage = $('#selectPage');
                    $selectPage.empty();
                    let e = '';
                    for (let i = 1; i <= allPage; i++) {
                        e += '<option value="' + i + '">' + i + '</option>';
                    }
                    $selectPage.append(e);
                    //总页码
                    $(".allPage").html("共" + allPage + "页");
                }
                //限制上一页
                if (loadParams.pageNum == 1) {
                    $(".upPageDiv").html('上一页');
                } else {
                    $(".upPageDiv").html('<a href="javascript:updatePage(' + (parseInt(loadParams.pageNum) - 1) + ')">上一页</a>');
                }
                //限制下一页
                if (loadParams.pageNum >= allPage) {
                    $(".downPageDiv").html('下一页');
                } else {
                    $(".downPageDiv").html('<a href="javascript:updatePage(' + (parseInt(loadParams.pageNum) + 1) + ')">下一页</a>');
                }
                //修改当前页码
                $("#selectPage").val(loadParams.pageNum);
            } else if (data.errCode === -1) {
                window.parent.WeKnow.location("loginTimeout");
            } else {
                WeKnow.errorMsg(data.errMsg);
            }
        }, complete: function () {
            WeKnow.closeLoading();
        }
    });

}

//查看模板
$("#tbodyTemplateList").on('click', '.showPrescriptionTemplateSpan', function (e) {
    //获取数据
    let $parentTr = $(this).closest('tr');
    MY_SHOW_IMG.showImg($parentTr.data('template_pic_url'), 1);
});


//下载图片
$("#tbodyTemplateList").on('click', '.downLoadTemplateSpan', function (e) {
    //获取数据
    let $parentTr = $(this).closest('tr');
    let path = $parentTr.data('template_pic_url');
    let imgName = $parentTr.data('template_name');
    downloadImage(path, imgName);
});

function downloadImage(path, imgName) {
    var _OBJECT_URL;
    var request = new XMLHttpRequest();
    request.addEventListener('readystatechange', function (e) {
        if (request.readyState == 4) {
            _OBJECT_URL = URL.createObjectURL(request.response);
            let $a = $("<a></a>").attr("href", _OBJECT_URL).attr("download", imgName);
            $a[0].click();
        }
    });
    request.responseType = 'blob';
    request.open('get', path);
    request.send();
}

//设置
$("#tbodyTemplateList").on('click', '.setTemplateSpan', function (e) {
    //获取数据
    let $parentTr = $(this).closest('tr');
    let institutionId = $parentTr.data('institution_id');
    $("#institutionId").val(institutionId);
    $('#updatePrescriptionTemplateDiv').modal('show');
});

//修改处方模板templateSign
$("#updatePrescriptionTemplate").on('click', function (e) {
    let templateSign = $("#templateSign").val();

    if (!templateSign) {
        WeKnow.errorMsg('参数必填');
        return false;
    }
    let institutionId = $("#institutionId").val();
    let data = {};
    data.templateSign = templateSign;
    data.institutionId = institutionId;
    WeKnow.loading();
    $.ajax({
        type: "POST",
        url: "prescriptionAudit/updateTemplateSign",
        dataType: "json",
        data: data,
        async: true,//异步调用
        success: function (data) {
            WeKnow.closeLoading();
            if (!WeKnow.filterAjaxResult(data)) return false; // session过期check
            if (data.errCode == 1) {
                window.location.reload();
            } else if (data.errCode == 0) {
                WeKnow.errorMsg(data.errMsg);
            } else {
                WeKnow.systemMsg('查询失败，请重新登录后再试');
            }
        }
    });
});


//获取 加载数据集合 入参
function getLoadParams() {
    let params = {};
    //账号名称
    params.institutionalName = $("#institutionalName").val();
    params.templateName = $("#templateName").val();
    params.templateSign = $('#templateSignType').val();
    //页面
    params.pageNum = $("#pageNum").val();
    //一页数据加载数(limit)
    params.pageSize = $("#pageSize").val();
    return params;
}

// 搜索按钮
$("#search").on('click', function (e) {
    //重置页码
    $("#pageNum").val(1);
    //查询数据
    loadData();
});


//页面变更
$("#selectPage").on('change', function (e) {
    updatePage($(this).val())
});


//修改页码
function updatePage(pageNum) {
    //获取页码
    $("#pageNum").val(pageNum);
    //查询数据
    loadData();
}


//limit变更
$("#pageSize").on('change', function (e) {
    //重置页码
    $("#pageNum").val(1);
    //查询数据
    loadData();
});